package com.surelution.lansao.wxmp.partner;

import com.surelution.lansao.PartnerRequiredOperation

public class PartnerRequiredAction extends BaseLansaoPartnerAction {
	
	private PartnerRequiredOperation operation

	@Override
	public boolean accept() {
		operation = null
		println partner.id
		def cri = PartnerRequiredOperation.createCriteria()
		def result = cri.list {
			eq('partner', partner)
			or {
				eq('forever', true)
				ge('expiredAt', new Date())
			}
		}
		if(result && result.size()) {
			operation = result[0]
		}
		return operation != null;
	}

	@Override
	public void execute() {
		def className = operation.className
		def clazz = getClass().classLoader.loadClass(className)
		RequiredBaseSub o = clazz.newInstance()
		o.host = this
		o.operation = operation
		if(o.accept()) {
			def attrs = o.process()
			if(attrs) {
				for(def attr : attrs) {
					put(attr)
				}
			}
		}
	}

}
